/*
Copyright 2008, 2009 Joonas Lehtolahti

This file is part of Openfire-C.

Openfire-C is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Openfire-C is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with Openfire-C.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef _XFIREPACKET_H_
#define _XFIREPACKET_H_

#include "../shared/dllexport.h"
#include "../shared/non0string.h"
#include "../shared/linkedlist.h"
#include "../shared/tree.h"
#include "../shared/helpers.h"
#include "XfirePacketID.h"

#ifndef _WIN32
#define XfirePattrFree_internal(o) XfirePattrFree(o)
#endif

#define XFP_PACKET_MAXSIZE 0xffff

#define XFP_SIZE_PACKETLENGTH 2
#define XFP_MASK_PACKETLENGTH 0xffff
#define XFP_SIZE_PACKETID 2
#define XFP_MASK_PACKETID 0xffff
#define XFP_SIZE_ATTRIBUTECOUNT 1
#define XFP_MASK_ATTRIBUTECOUNT 0xff
#define XFP_SIZE_HEADER XFP_SIZE_PACKETLENGTH + XFP_SIZE_PACKETID + XFP_SIZE_ATTRIBUTECOUNT
#define XFP_SIZE_ATTRIBUTENAMELENGTH 1
#define XFP_MASK_ATTRIBUTENAMELENGTH 0xff
#define XFP_SIZE_SHORTATTRIBUTENAME 1
#define XFP_MASK_SHORTATTRIBUTENAME 0xff
#define XFP_SIZE_ATTRIBUTETYPE 1
#define XFP_MASK_ATTRIBUTETYPE 0xff

#define XFP_SIZE_ATTRINTVALUE 4
#define XFP_MASK_ATTRINTVALUE 0xffffffff
#define XFP_SIZE_ATTRBOOLEANVALUE 1
#define XFP_MASK_ATTRBOOLEANVALUE 0xff
#define XFP_SIZE_ATTRLONGVALUE 8
#define XFP_MASK_ATTRLONGVALUE 0xffffffffffffffffLL
#define XFP_SIZE_ATTRSTRINGLENGTH 2
#define XFP_MASK_ATTRSTRINGLENGTH 0xffff
#define XFP_SIZE_ATTRSIDVALUE 16
#define XFP_SIZE_ATTRGCIDVALUE 21
#define XFP_SIZE_ATTRLISTLENGTH 2
#define XFP_MASK_ATTRLISTLENGTH 0xffff

#define XFP_TYPE_STRING 0x01
#define XFP_TYPE_INT 0x02
#define XFP_TYPE_SID 0x03
#define XFP_TYPE_LIST 0x04
#define XFP_TYPE_PACKET 0x05
#define XFP_TYPE_GCID 0x06
#define XFP_TYPE_LONG 0x07
#define XFP_TYPE_BOOLEAN 0x08
#define XFP_TYPE_NPACKET 0x09

#ifdef DEBUG
        #define xfdbgprintf(level, x) if(inst->debug>=level) printf x
#else
        #define xfdbgprintf(level, x)
#endif

struct XfirePacket
{
        unsigned int    length;     /* Length of the network packet */
        unsigned int    pid;        /* Packet ID number */
        unsigned int    attrs;      /* Number of attributes */
                 int    shortnames; /* Are the attribute names short or long */
   struct XfirePattr ** attributes; /* Pointer to a table of attribute pointers */
};

struct XfirePattr
{
        unsigned int    aid;        /* Attribute type */
                void  * attr;       /* Attribute value */
          non0string  * name;       /* Attribute name */
};

struct XfirePattr4
{
        unsigned int    aid;        /* Attributes type */
        unsigned int    size;       /* Number of attributes in the list */
                void ** attr;       /* Attribute value */
};


DLLEXPORT(struct XfirePacket*) XfirePacketParse(non0string *data);
DLLEXPORT(struct XfirePacket*) XfirePacketCreate(unsigned int attributes, int shortnames);
DLLEXPORT(unsigned int) XfirePacketGetPID(non0string *data);

DLLEXPORT(void) XfirePacketFree(struct XfirePacket *obj);
void XfirePattrFree_internal(struct XfirePattr *obj);
DLLEXPORT(void) XfirePattr4Free(struct XfirePattr4 *obj);

DLLEXPORT(non0string*) XfireStreamFromPacket(struct XfirePacket *packet);

#endif
